package com.spring.dao.example.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.spring.bean.example.Book;
import com.spring.bean.page.Page;
import com.spring.bean.page.PageView;
import com.spring.dao.DaoSupport;
import com.spring.dao.example.BookDao;
import com.spring.exception.DaoException;
import com.spring.utils.Constants;

@Repository
public class BookDaoImpl extends DaoSupport implements BookDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public PageView<Book> queryForPage(Page page) throws DaoException {
		String sql = "SELECT " + Constants.SQL_PAGE_KEY + " * from book";
		//return queryCacheForPage(jdbcTemplate, Book.class, sql, page, true);
		return queryForPage(jdbcTemplate, Book.class, sql, page);
	}

	@Override
	public int save(Book book) throws DaoException {
		insert(jdbcTemplate, book);
		int theLastId = (Integer) jdbcTemplate.queryForObject("SELECT LAST_INSERT_ID()", Integer.class);
		System.out.println(theLastId);
		return theLastId;
	}

	@Override
	public boolean update(Book book) throws DaoException {
		return update(jdbcTemplate, book);
	}

	@Override
	public boolean delete(Book book) throws DaoException {
		return delete(jdbcTemplate, book);
	}

	@Override
	public Book getById(Integer id) throws DaoException {
		return getById(jdbcTemplate, Book.class, id);
	}
	
  }
